package com.ruoyi.fjtseac.service

import com.baomidou.mybatisplus.core.conditions.Wrapper
import com.ruoyi.common.constant.HttpStatus
import com.ruoyi.common.core.redis.RedisCache
import com.ruoyi.fjtseac.domain.dto.AudioDto
import com.ruoyi.fjtseac.domain.dto.VideoDto
import com.ruoyi.fjtseac.domain.po.Audio
import com.ruoyi.fjtseac.domain.po.Video
import com.ruoyi.fjtseac.mapper.AudioMapper
import com.ruoyi.fjtseac.service.impl.AudioServiceImpl
import com.ruoyi.fjtseac.util.Constants
import com.ruoyi.fjtseac.util.FileUtil
import com.ruoyi.fjtseac.util.LocalFile
import org.dbunit.database.IResultSetTable
import org.mockito.Mock
import org.mockito.Mockito
import org.powermock.api.mockito.PowerMockito
import org.springframework.mock.web.MockMultipartFile
import org.springframework.web.multipart.MultipartFile
import spock.lang.Specification
import spock.lang.Unroll

/**
 *
 * @author zzl
 * @date 2022/8/16 23:03
 */
class AudioServiceTest extends Specification {

    def fileUtil = Mock(FileUtil)
    def localFile = Mock(LocalFile)
    def audioMapper = Mock(AudioMapper)
    def audioServiceImpl = new AudioServiceImpl(fileUtil: fileUtil, localFile: localFile, audioMapper: audioMapper)

    @Unroll
    def "upload(audio: #audioDto, String uuid), msg: #resultMsg"() {
        given:
        fileUtil.upload(audioDto.getAudioFile(), Constants.AUDIO, null) >> audioUrl
        localFile.getFileTime(audioDto.getAudioFile(), Constants.AUDIO) >> duration
        audioMapper.insert(_ as Audio) >> num

        when:
        def response = audioServiceImpl.upload(audioDto, null)

        then:
        with(response) {
            msg == resultMsg
        }

        where:
        audioDto                                                             | audioUrl                         | duration | num || httpStatus         | resultMsg
        newAudioDto("你给我听好", "D:\\QQMusic\\张碧晨 - 你给我听好 (Live).mp3")           | "http://cos.guangdong.cn/audio1" | "125"   | 1 || HttpStatus.SUCCESS | "操作成功"
        newAudioDto("千年泪", "D:\\45568f0582117caac65df45e03600d9d.jpeg")       | Constants.FALSE                  | "278"   | 1 || HttpStatus.ERROR   | "音频文件因某种原因上传失败，请重新上传"
        newAudioDto("风", "C:\\Users\\zzl\\Desktop\\七叔-叶泽浩 - 客子光阴 七叔现场版.mp4")  | "http://cos.guangdong.cn/audio2" | null    | 1 || HttpStatus.ERROR   | "获取音频时长失败"
        newAudioDto("音频", "C:\\Users\\zzl\\Desktop\\七叔-叶泽浩 - 客子光阴 七叔现场版.mp4") | "http://cos.guangdong.cn/audio3" | "06:40" | 0 || HttpStatus.ERROR   | "数据插入失败"
    }

    def newAudioDto(def title, def fileUrl) {
        return new AudioDto(audioName: title, audioFile: newMulFile(fileUrl))
    }

    MultipartFile newMulFile(def fileUrl) {
        File file  = new File(fileUrl)
        FileInputStream input = new FileInputStream(file)
        MultipartFile multipartFile = new MockMultipartFile(file.getName(),file.getName(), "audio/mp3", input)
        return multipartFile
    }

    @Unroll
    def "delete(id: #id)"() {
        given:
        audioMapper.selectById(id) >> audio
        fileUtil.delete(_ as String) >> flag

        when:
        def response = audioServiceImpl.delete(id)


        then:
        with(response) {
            msg == resultMsg
        }

        where:
        id | audio                                   | flag  || httpStatus         | resultMsg
        0  | null                                    | true  || HttpStatus.ERROR   | "音频记录不存在"
        1  | new Audio(audioId: 1, audioUrl: "http") | false || HttpStatus.ERROR   | "音频记录删除成功，但音频文件删除失败"
        2  | new Audio(audioId: 2, audioUrl: "http") | true  || HttpStatus.SUCCESS | "操作成功"
    }

    @Unroll
    def "search(param: #param, pageNum: pageNum, pageSize: pageSize), code: httpStatus"() {
        given:
        audioMapper.selectList(_ as Wrapper<Audio>) >> resultSet

        when:
        def response = audioServiceImpl.search(param, pageNum, pageSize);

        then:
        with(response) {
            code == httpStatus
            response.get("data").getAt("total") == total;
        }

        where: "输入输出数据表格"
        param | pageNum | pageSize | resultSet                                                || httpStatus         | pageCount | total
        null  | 1       | 10       | [new Audio(audioName: "天后"), new Audio(audioName: "后来")] || HttpStatus.SUCCESS | 1         | 2
        "后"   | 2       | 2        | [new Audio(audioName: "天后"), new Audio(audioName: "后来")] || HttpStatus.SUCCESS | 1         | 2
        "天"   | 2       | 2        | [new Audio(audioName: "天后")]                             || HttpStatus.SUCCESS | 1         | 1
        "1"   | 1       | 5        | []                                                       || HttpStatus.SUCCESS | 1         | 0
    }
}
